package com.lc.pianostudent.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.AnswerResultBean;
import com.lc.pianostudent.conn.Conn;
import com.lc.pianostudent.conn.PostAnswerIndex;
import com.lc.pianostudent.conn.PostChangePassword;
import com.lc.pianostudent.fragment.FindFragment;
import com.lc.pianostudent.util.Util;
import com.lc.pianostudent.view.CommonPopupWindow;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/27.
 */

public class EverydayAnswerActivity extends BaseActivity implements View.OnClickListener,CommonPopupWindow.ViewInterface{

    @BoundView(value = R.id.look_rule,isClick = true) private LinearLayout look_rule;
    @BoundView(value = R.id.friend_list_layout,isClick = true) private LinearLayout friend_list_layout;
    @BoundView(value = R.id.junior_layout,isClick = true) private ImageView junior_layout;
    @BoundView(value = R.id.middle_layout,isClick = true) private ImageView middle_layout;
    @BoundView(value = R.id.high_layout,isClick = true) private ImageView high_layout;
    @BoundView(value = R.id.question_back_layout,isClick = true) private LinearLayout question_back_layout;
    @BoundView(R.id.xuewei) private TextView xuewei;
    @BoundView(R.id.degree) private TextView degree;
    private PostAnswerIndex.PostAnswerIndexInfo currentInfo;
    private SharedPreferences sp ;
    private CommonPopupWindow popupWindow;
    private List<AnswerResultBean> list = new ArrayList<>();
    private List<ArrayList<String>> list1 = new ArrayList<>();
    private int total;
    //每日答题
    public PostAnswerIndex postAnswerIndex = new PostAnswerIndex(new AsyCallBack<PostAnswerIndex.PostAnswerIndexInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostAnswerIndex.PostAnswerIndexInfo postAnswerIndexInfo) throws Exception {
//            Log.e("===",postAnswerIndexInfo.code);
            xuewei.setText(postAnswerIndexInfo.xuewei);
            degree.setText(postAnswerIndexInfo.degree);
            BaseApplication.BasePreferences.saveNianji(postAnswerIndexInfo.xuewei);
            BaseApplication.BasePreferences.saveFenshu(postAnswerIndexInfo.degree);
            BaseApplication.BasePreferences.saveDegreeType(postAnswerIndexInfo.type);
            currentInfo = postAnswerIndexInfo;
            BaseApplication.BasePreferences.saveTeacherId(postAnswerIndexInfo.teacherid);
            if(postAnswerIndexInfo.code.equals("200")){
                total = 1;

            }else {
                total = 2;
            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            total = 2;
            xuewei.setText(BaseApplication.BasePreferences.readNianji());
            degree.setText(BaseApplication.BasePreferences.readFenshu());
//            UtilToast.show(toast);

        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_everyday_answer);
        setBackTrue();
        setAppCallBack(new CallBack());
        initView();
    }

    private void initView(){
        setTitleName("每日答题");
        sp = getSharedPreferences("User", Context.MODE_PRIVATE);

        postAnswerIndex.user_id = BaseApplication.BasePreferences.readUID();
        postAnswerIndex.execute();


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.look_rule:
                startActivity(new Intent(EverydayAnswerActivity.this, WebActivity.class).putExtra("url", Conn.SERVICE+Conn.WEB_SHOW_ANSWER).putExtra("title", "查看答题规则"));
                break;

            case R.id.friend_list_layout:
                startVerifyActivity(FriendListActivity.class);

                break;

            case R.id.junior_layout:

                if(total == 1){
                    startActivity(new Intent(EverydayAnswerActivity.this,QuestionActivity.class).putExtra("type","1"));

                }else {
                     //1.从答题页进入  2.从每日答题进入
                    UtilToast.show("今日已答题");
                    startActivity(new Intent(EverydayAnswerActivity.this,AnswerResultActivity.class).putExtra("from","2").putExtra("type","1"));
                }
                break;
            case R.id.middle_layout:
                if(BaseApplication.BasePreferences.readDegreeType().equals("1")){
                    UtilToast.show("您当前的等级为初级，不可以答中级的题哦~");
                }else {

                    if(total == 1){
                        startActivity(new Intent(EverydayAnswerActivity.this,QuestionActivity.class).putExtra("type","2"));

                    }else {
                         UtilToast.show("今日已答题");
                         //1.从答题页进入  2.从每日答题进入
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerResultActivity.class).putExtra("from","2").putExtra("type","2"));
                    }
                }

                break;
            case R.id.high_layout:
                if(BaseApplication.BasePreferences.readDegreeType().equals("1")){
                    UtilToast.show("您当前的等级为初级，不可以答高级的题哦~");
                } else if(BaseApplication.BasePreferences.readDegreeType().equals("2")){
                     UtilToast.show("您当前的等级为中级，不可以答高级的题哦~");
                }else {
                    if(total == 1){
                        startActivity(new Intent(EverydayAnswerActivity.this,QuestionActivity.class).putExtra("type","3"));

                    }else {
                         UtilToast.show("今日已答题");
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerResultActivity.class).putExtra("from","2").putExtra("type","3"));
                    }                }
                break;

            case R.id.question_back_layout:
//                Log.e("=====size",list.size()+"---");
                String data = sp.getString("listStr", "");
                if (!TextUtils.isEmpty(data)) {
                    //题目id集合
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<AnswerResultBean>>() {
                    }.getType();
                    list = gson.fromJson(data, listType);
                    //我选的答案集合
                    String liststr = sp.getString("mylistStr", "");

                    if (!TextUtils.isEmpty(liststr)) {
                        try {
                            list1 = Util.String2SceneList(liststr);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    showCustomizeDialog();
                }else {
                    UtilToast.show("您还没有答题哦~");
                }



                break;
        }
    }

    private void showCustomizeDialog() {
    /* @setView 装入自定义View ==> R.layout.dialog_customize
     * 由于dialog_customize.xml只放置了一个EditView，因此和图8一样
     * dialog_customize.xml可自定义更复杂的View
     */
        if (popupWindow != null && popupWindow.isShowing()) return;
        popupWindow = new CommonPopupWindow.Builder(EverydayAnswerActivity.this)
                .setView(R.layout.dialog_back_answer)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setBackGroundLevel(0.8f)//取值范围0.0f-1.0f 值越小越暗
                //.setAnimationStyle(R.style.popWindow_anim_style)
                .setViewOnclickListener(this)
                .create();
        popupWindow.showAtLocation(EverydayAnswerActivity.this.findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.dialog_back_answer:
                TextView corrent_count = (TextView) view.findViewById(R.id.corrent_count);
                TextView wrong_count = (TextView) view.findViewById(R.id.wrong_count);
                TextView one_text = (TextView) view.findViewById(R.id.one_text);
                TextView two_text = (TextView) view.findViewById(R.id.two_text);
                TextView three_text = (TextView) view.findViewById(R.id.three_text);
                TextView four_text = (TextView) view.findViewById(R.id.four_text);
                TextView five_text = (TextView) view.findViewById(R.id.five_text);
                TextView six_text = (TextView) view.findViewById(R.id.six_text);
                TextView seven_text = (TextView) view.findViewById(R.id.seven_text);
                TextView eight_text = (TextView) view.findViewById(R.id.eight_text);
                TextView nine_text = (TextView) view.findViewById(R.id.nine_text);
                TextView ten_text = (TextView) view.findViewById(R.id.ten_text);

                ImageView one_img = (ImageView) view.findViewById(R.id.one_img);
                ImageView two_img = (ImageView) view.findViewById(R.id.two_img);
                ImageView three_img = (ImageView) view.findViewById(R.id.three_img);
                ImageView four_img = (ImageView) view.findViewById(R.id.four_img);
                ImageView five_img = (ImageView) view.findViewById(R.id.five_img);
                ImageView six_img = (ImageView) view.findViewById(R.id.six_img);
                ImageView seven_img = (ImageView) view.findViewById(R.id.seven_img);
                ImageView eight_img = (ImageView) view.findViewById(R.id.eight_img);
                ImageView nine_img = (ImageView) view.findViewById(R.id.nine_img);
                ImageView ten_img = (ImageView) view.findViewById(R.id.ten_img);
                final ImageView dismiss = view.findViewById(R.id.dismiss);
                corrent_count.setText(BaseApplication.BasePreferences.readCorrect());
                wrong_count.setText(BaseApplication.BasePreferences.readWrong());
                //第一题
                if(list.get(0).getContent().equals("true")){
                    one_img.setImageResource(R.mipmap.review_right);
                    one_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    one_img.setImageResource(R.mipmap.review_wrong);
                    one_text.setTextColor(Color.parseColor("#f63f22"));
                }
                //第二题
                if(list.get(1).getContent().equals("true")){
                    two_img.setImageResource(R.mipmap.review_right);
                    two_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    two_img.setImageResource(R.mipmap.review_wrong);
                    two_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第三题
                if(list.get(2).getContent().equals("true")){
                    three_img.setImageResource(R.mipmap.review_right);
                    three_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    three_img.setImageResource(R.mipmap.review_wrong);
                    three_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第四题
                if(list.get(3).getContent().equals("true")){
                    four_img.setImageResource(R.mipmap.review_right);
                    four_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    four_img.setImageResource(R.mipmap.review_wrong);
                    four_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第五题
                if(list.get(4).getContent().equals("true")){
                    five_img.setImageResource(R.mipmap.review_right);
                    five_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    five_img.setImageResource(R.mipmap.review_wrong);
                    five_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第六题
                if(list.get(5).getContent().equals("true")){
                    six_img.setImageResource(R.mipmap.review_right);
                    six_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    six_img.setImageResource(R.mipmap.review_wrong);
                    six_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第七题
                if(list.get(6).getContent().equals("true")){
                    seven_img.setImageResource(R.mipmap.review_right);
                    seven_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    seven_img.setImageResource(R.mipmap.review_wrong);
                    seven_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第八题
                if(list.get(7).getContent().equals("true")){
                    eight_img.setImageResource(R.mipmap.review_right);
                    eight_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    eight_img.setImageResource(R.mipmap.review_wrong);
                    eight_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第九题
                if(list.get(8).getContent().equals("true")){
                    nine_img.setImageResource(R.mipmap.review_right);
                    nine_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    nine_img.setImageResource(R.mipmap.review_wrong);
                    nine_text.setTextColor(Color.parseColor("#f63f22"));
                }

                //第九题
                if(list.get(9).getContent().equals("true")){
                    ten_img.setImageResource(R.mipmap.review_right);
                    ten_text.setTextColor(Color.parseColor("#02c422"));
                }else {
                    ten_img.setImageResource(R.mipmap.review_wrong);
                    ten_text.setTextColor(Color.parseColor("#f63f22"));
                }


                one_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(0).getId()).putExtra("count","1")
                        .putStringArrayListExtra("select",list1.get(0)));
                    }
                });
                two_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(1).getId()).putExtra("count","2")
                                .putStringArrayListExtra("select",list1.get(1)));
                    }
                });
                three_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(2).getId()).putExtra("count","3")
                                .putStringArrayListExtra("select",list1.get(2)));
                    }
                });
                four_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(3).getId()).putExtra("count","4")
                                .putStringArrayListExtra("select",list1.get(3)));
                    }
                });
                five_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(4).getId()).putExtra("count","5")
                                .putStringArrayListExtra("select",list1.get(4)));
                    }
                });

                six_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(5).getId()).putExtra("count","6")
                                .putStringArrayListExtra("select",list1.get(5)));
                    }
                });

                seven_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(6).getId()).putExtra("count","7")
                                .putStringArrayListExtra("select",list1.get(6)));
                    }
                });

                eight_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(7).getId()).putExtra("count","8")
                                .putStringArrayListExtra("select",list1.get(7)));
                    }
                });

                nine_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(8).getId()).putExtra("count","9")
                                .putStringArrayListExtra("select",list1.get(8)));
                    }
                });

                ten_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(EverydayAnswerActivity.this,AnswerBackActivity.class)
                                .putExtra("id",list.get(9).getId()).putExtra("count","10")
                                .putStringArrayListExtra("select",list1.get(9)));
                    }
                });
                dismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                    }
                });
                break;
        }
    }

    public class CallBack implements AppCallBack {

        public void onRefresh() {
            postAnswerIndex.user_id = BaseApplication.BasePreferences.readUID();
            postAnswerIndex.execute();
        }

    }
// ┏┓　　　┏┓
// ┏┛┻━━━┛┻┓
// ┃　　　　　　　┃ 　
// ┃　　　━　　　┃
// ┃　┳┛　┗┳　┃
// ┃　　　　　　　┃
// ┃　　　┻　　　┃
// ┃　　　　　　　┃
// ┗━┓　　　┏━┛
// ┃　　　┃ 神兽保佑　　　　　　　　
// ┃　　　┃ 代码无BUG！
// ┃　　　┗━━━┓
// ┃　　　　　　　┣┓
// ┃　　　　　　　┏┛
// ┗┓┓┏━┳┓┏┛
// ┃┫┫　┃┫┫
// ┗┻┛　┗┻┛
}
